package com.cr.aio;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class AIOServer {
    //线程池，提高服务端效率
    private ExecutorService service;
//    //线程组，
//    private AsynchronousChannelGroup group;
    //服务端通道，针对服务端定义的通道
    private AsynchronousServerSocketChannel serverChannel;
    public AIOServer(int port){

    }
    private  void init(int port){
        try{
            System.out.println("server starting at port : "+port+"...");
            //定长线程池
            service= Executors.newFixedThreadPool(4);
            /*
            使用线程组
            group=AsynchronousChannelGroup.withThreadPool(service);
            serverChannel=AsynchronousServerSocketChannel.open(group);
             */
            //开启服务端通道 通过静态方法创建的
            serverChannel=AsynchronousServerSocketChannel.open();
            //绑定监听端口
            serverChannel.bind(new InetSocketAddress(port));
            System.out.println("server started...");
            //开始监听
            //serverChannel.accept(this,);
            try{
                TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
